package c.e.a.b.f.b;

import android.content.Context;
import android.os.Binder;
import android.os.Bundle;
import android.text.TextUtils;
import c.e.a.b.b.e;
import c.e.a.b.b.f;
import c.e.a.b.b.j.c;
import c.e.a.b.b.j.d;
import c.e.a.b.e.c.ga;
import c.e.a.b.e.c.v8;
import com.google.android.gms.measurement.internal.zzaq;
import com.google.android.gms.measurement.internal.zzku;
import com.google.android.gms.measurement.internal.zzn;
import com.google.android.gms.measurement.internal.zzz;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/* compiled from: com.google.android.gms:play-services-measurement@@18.0.0 */
public final class z4 extends l3 {
    public final k9 a;
    public Boolean b;
    public String c = null;

    public z4(k9 k9Var) {
        Objects.requireNonNull(k9Var, "null reference");
        this.a = k9Var;
    }

    public final void C0(long j, String str, String str2, String str3) {
        K0(new p5(this, str2, str3, str, j));
    }

    public final void D0(zzku zzku, zzn zzn) {
        Objects.requireNonNull(zzku, "null reference");
        M0(zzn);
        K0(new o5(this, zzku, zzn));
    }

    public final void E0(zzn zzn) {
        L0(zzn.a, false);
        K0(new h5(this, zzn));
    }

    public final List<zzz> F0(String str, String str2, String str3) {
        L0(str, true);
        try {
            return (List) ((FutureTask) this.a.a().t(new i5(this, str, str2, str3))).get();
        } catch (InterruptedException | ExecutionException e) {
            this.a.d().f.b("Failed to get conditional user properties as", e);
            return Collections.emptyList();
        }
    }

    public final void G0(zzz zzz, zzn zzn) {
        String str = "null reference";
        Objects.requireNonNull(zzz, str);
        Objects.requireNonNull(zzz.c, str);
        M0(zzn);
        zzz zzz2 = new zzz(zzz);
        zzz2.a = zzn.a;
        K0(new b5(this, zzz2, zzn));
    }

    public final List<zzz> H0(String str, String str2, zzn zzn) {
        M0(zzn);
        try {
            return (List) ((FutureTask) this.a.a().t(new f5(this, zzn, str, str2))).get();
        } catch (InterruptedException | ExecutionException e) {
            this.a.d().f.b("Failed to get conditional user properties", e);
            return Collections.emptyList();
        }
    }

    public final void J0(zzz zzz) {
        String str = "null reference";
        Objects.requireNonNull(zzz, str);
        Objects.requireNonNull(zzz.c, str);
        L0(zzz.a, true);
        K0(new e5(this, new zzz(zzz)));
    }

    public final void K0(Runnable runnable) {
        if (this.a.a().y()) {
            runnable.run();
        } else {
            this.a.a().v(runnable);
        }
    }

    public final void L0(String str, boolean z) {
        if (TextUtils.isEmpty(str)) {
            String str2 = "Measurement Service called without app package";
            this.a.d().f.a(str2);
            throw new SecurityException(str2);
        }
        if (z) {
            try {
                if (this.b == null) {
                    if (!("com.google.android.gms".equals(this.c) || d.d(this.a.j.a, Binder.getCallingUid()))) {
                        if (!f.a(this.a.j.a).b(Binder.getCallingUid())) {
                            z = false;
                            this.b = Boolean.valueOf(z);
                        }
                    }
                    z = true;
                    this.b = Boolean.valueOf(z);
                }
                if (this.b.booleanValue()) {
                    return;
                }
            } catch (SecurityException e) {
                this.a.d().f.b("Measurement Service called with invalid calling package. appId", q3.s(str));
                throw e;
            }
        }
        if (this.c == null) {
            Context context = this.a.j.a;
            int callingUid = Binder.getCallingUid();
            boolean z2 = e.a;
            if (d.f(context, callingUid, str)) {
                this.c = str;
            }
        }
        if (!str.equals(this.c)) {
            throw new SecurityException(String.format("Unknown calling package name '%s'.", new Object[]{str}));
        }
    }

    public final void M0(zzn zzn) {
        Objects.requireNonNull(zzn, "null reference");
        L0(zzn.a, false);
        this.a.j.t().c0(zzn.b, zzn.r, zzn.v);
    }

    public final List<zzku> X(String str, String str2, boolean z, zzn zzn) {
        M0(zzn);
        try {
            List<u9> list = (List) ((FutureTask) this.a.a().t(new d5(this, zzn, str, str2))).get();
            ArrayList arrayList = new ArrayList(list.size());
            for (u9 u9Var : list) {
                if (z || !t9.r0(u9Var.c)) {
                    arrayList.add(new zzku(u9Var));
                }
            }
            return arrayList;
        } catch (InterruptedException | ExecutionException e) {
            this.a.d().f.c("Failed to query user properties. appId", q3.s(zzn.a), e);
            return Collections.emptyList();
        }
    }

    public final void Y(zzn zzn) {
        M0(zzn);
        K0(new q5(this, zzn));
    }

    public final byte[] Z(zzaq zzaq, String str) {
        b.i.b.f.N(str);
        Objects.requireNonNull(zzaq, "null reference");
        L0(str, true);
        this.a.d().m.b("Log and bundle. event", this.a.O().u(zzaq.a));
        Objects.requireNonNull((c) this.a.j.n);
        long nanoTime = System.nanoTime() / 1000000;
        r4 a = this.a.a();
        Callable l5Var = new l5(this, zzaq, str);
        a.o();
        s4 s4Var = new s4(a, l5Var, true, "Task exception on worker thread");
        if (Thread.currentThread() == a.c) {
            s4Var.run();
        } else {
            a.u(s4Var);
        }
        try {
            byte[] bArr = (byte[]) s4Var.get();
            if (bArr == null) {
                this.a.d().f.b("Log and bundle returned null. appId", q3.s(str));
                bArr = new byte[0];
            }
            Objects.requireNonNull((c) this.a.j.n);
            this.a.d().m.d("Log and bundle processed. event, size, time_ms", this.a.O().u(zzaq.a), Integer.valueOf(bArr.length), Long.valueOf((System.nanoTime() / 1000000) - nanoTime));
            return bArr;
        } catch (InterruptedException | ExecutionException e) {
            this.a.d().f.d("Failed to log and bundle. appId, event, error", q3.s(str), this.a.O().u(zzaq.a), e);
            return null;
        }
    }

    public final void a0(zzn zzn) {
        if (v8.a() && this.a.j.g.o(p.J0)) {
            b.i.b.f.N(zzn.a);
            Objects.requireNonNull(zzn.w, "null reference");
            k5 k5Var = new k5(this, zzn);
            if (this.a.a().y()) {
                k5Var.run();
                return;
            }
            this.a.a().w(k5Var);
        }
    }

    public final void j0(zzn zzn) {
        M0(zzn);
        K0(new c5(this, zzn));
    }

    public final List<zzku> k0(String str, String str2, String str3, boolean z) {
        L0(str, true);
        try {
            List<u9> list = (List) ((FutureTask) this.a.a().t(new g5(this, str, str2, str3))).get();
            ArrayList arrayList = new ArrayList(list.size());
            for (u9 u9Var : list) {
                if (z || !t9.r0(u9Var.c)) {
                    arrayList.add(new zzku(u9Var));
                }
            }
            return arrayList;
        } catch (InterruptedException | ExecutionException e) {
            this.a.d().f.c("Failed to get user properties as. appId", q3.s(str), e);
            return Collections.emptyList();
        }
    }

    public final void t0(zzaq zzaq, zzn zzn) {
        Objects.requireNonNull(zzaq, "null reference");
        M0(zzn);
        K0(new j5(this, zzaq, zzn));
    }

    public final String u0(zzn zzn) {
        M0(zzn);
        k9 k9Var = this.a;
        try {
            return (String) ((FutureTask) k9Var.j.a().t(new o9(k9Var, zzn))).get(30000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            k9Var.j.d().f.c("Failed to get app instance id. appId", q3.s(zzn.a), e);
            return null;
        }
    }

    public final void v0(Bundle bundle, zzn zzn) {
        if (ga.a() && this.a.j.g.o(p.A0)) {
            M0(zzn);
            K0(new y4(this, zzn, bundle));
        }
    }
}
